package org.dev.hi.hiui.tab.bottom;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.dev.hi.hiui.R;
import org.dev.hi.hiui.tab.common.IHiTab;

public
/**
 * Author WenPing
 * CreateTime 2021/3/7.
 * Description:
 */

class HiTabBottom extends RelativeLayout implements IHiTab<HiTabBottomInfo<?>> {

    //tabInfo信息
    private HiTabBottomInfo<?> tabInfo;
    //tab的imageView
    private ImageView tabImageView;
    //tab的名字
    private TextView tabNameView;
    //tab的icon图标
    private TextView tabIconView;

    /**
     * 构造方法
     * @param context
     */
    public HiTabBottom(Context context) {
        this(context, null);
    }

    public HiTabBottom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public HiTabBottom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //实现view的初始化
    private void init() {
        //hi_tab_bottom 的布局填充到当前的相对布局中
        LayoutInflater.from(getContext()).inflate(R.layout.hi_tab_bottom, this);
        tabImageView = findViewById(R.id.iv_image);
        tabIconView = findViewById(R.id.tv_icon);
        tabNameView = findViewById(R.id.tv_name);
    }

    @Override
    public void setHiTabInfo(@NonNull HiTabBottomInfo<?> tabInfo) {
        this.tabInfo = tabInfo;
        inflateInfo(false, true);
    }

    /**
     * 根据时候tabType分别实例化图标
     * 具体判断是否初始化
     * @param selected
     * @param init
     */
    private void inflateInfo(boolean selected, boolean init) {
        if (tabInfo.tabType == HiTabBottomInfo.TabType.ICON) {
            if (init) {
                tabImageView.setVisibility(GONE);
                tabIconView.setVisibility(VISIBLE);
                Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), tabInfo.iconFont);
                tabIconView.setTypeface(typeface);
                //设置tabName
                if (!TextUtils.isEmpty(tabInfo.name)) {
                    tabNameView.setText(tabInfo.name);
                }

            }
            if (selected) {
                tabIconView.setText(TextUtils.isEmpty(tabInfo.selectedIconName) ?
                        tabInfo.defaultIconName : tabInfo.selectedIconName);
                tabIconView.setTextColor(getTextColor(tabInfo.tintColor));
                tabNameView.setTextColor(getTextColor(tabInfo.tintColor));
            } else {
                tabIconView.setText(tabInfo.defaultIconName);
                tabIconView.setTextColor(getTextColor(tabInfo.defaultColor));
                tabNameView.setTextColor(getTextColor(tabInfo.defaultColor));
            }

        } else if (tabInfo.tabType == HiTabBottomInfo.TabType.BITMAP) {
            if (init) {
                tabImageView.setVisibility(VISIBLE);
                tabIconView.setVisibility(GONE);
                if (!TextUtils.isEmpty(tabInfo.name)) {
                    tabNameView.setText(tabInfo.name);
                }
            }
            if (selected) {
                tabImageView.setImageBitmap(tabInfo.selectedBitmap);
            } else {
                tabImageView.setImageBitmap(tabInfo.defaultBitmap);
            }
        }
    }

    /**
     * 根据颜色的类型进行对应的转换
     * @param color
     * @return
     */
    private int getTextColor(Object color) {
        if (color instanceof String) {
            return Color.parseColor((String) color);
        } else {
            return (int) color;
        }
    }

    @Override
    public void resetHeight(int height) {
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = height;
        setLayoutParams(layoutParams);
        //初始化TabInfo时 隐藏tabName
        getTabNameView().setVisibility(GONE);
    }

    public TextView getTabNameView() {
        return tabNameView;
    }

    @Override
    public void onTabSelectedChange(int index, @Nullable HiTabBottomInfo<?> prevInfo, @NonNull HiTabBottomInfo<?> nextInfo) {
        //prevInfo != tabInfo && nextInfo != tabInfo 如果被点击的和当前的tab无关，它就不需要变化
        // 或者前后两次点击的tabinfo是一样的，为避免反复实例化，进行return操作
        if (prevInfo != tabInfo && nextInfo != tabInfo || prevInfo == nextInfo) {
            return;
        }

        /**
         * 如果之前它是被选中的，那么这次它被反选了->变为未选中状态；
         * 反之，如果之前不是被选中的，那这次就是被选中了
         */
        if (prevInfo == tabInfo) {
            inflateInfo(false, false);
        }else {
            inflateInfo(true, false);
        }
    }

    public HiTabBottomInfo<?> getHiTabInfo() {
        return tabInfo;
    }

    public ImageView getTabImageView() {
        return tabImageView;
    }

    public TextView getTabIconView() {
        return tabIconView;
    }

}
